<template>
    <!-- 搜索 -->
    <div class="column">
        <div class="search">
            <a-input-search v-model:value="value" :placeholder="placeholder" enter-button size="large" @search="onSearch" />
        </div>
        <div class="search_type">
            <a-radio-group v-model:value="resource" @change="radioChange">
                <a-radio value="instation">站内</a-radio>
                <a-radio value="bing">必应</a-radio>
                <a-radio value="baidu">百度</a-radio>
                <a-radio value="google">Google</a-radio>
                <a-radio value="sogou">搜狗</a-radio>
                <a-radio value="360so">360搜索</a-radio>
                <a-radio value="kaifa">开发者搜索</a-radio>
            </a-radio-group>
        </div>
    </div>
    <!-- 站内搜索内容显示 -->
    <a-card v-if="searchCard" style="margin-bottom: 20px;">
        <h2>查询结果</h2>
        <div class="children-container">
            <div v-for="(info, index) in searchInfo" :key="index">
                <a :href="info.linkurl" target="_blank" class="card_href">
                    <a-card-grid class="a-card-grid">
                        <img :src="info.icon" @error="handleImageError"/>
                        <div class="card-text" :data-tooltip="info.name">{{info.name}}</div>
                    </a-card-grid>
                </a>
            </div>
        </div>
    </a-card>
    <!-- 初始数据加载 -->
    <a-card v-for="(item, index) in toolInfo" :key="index" style="margin-bottom: 20px;">
        <h2><img :src="item.icon">{{item.name}}</h2>
        <div class="children-container">
            <div v-for="(info, inx) in item.children" :key="inx">
                <a :href="info.linkurl" target="_blank" class="card_href">
                    <a-card-grid class="a-card-grid">
                        <img :src="info.icon" @error="handleImageError"/>
                        <div class="card-text" :data-tooltip="info.name">{{info.name}}</div>
                    </a-card-grid>
                </a>
            </div>
        </div>
    </a-card>
</template>

<script lang="ts" setup>
import { reactive,ref } from "vue";
import { jsondata } from '../../mock/JsonData'

//  控制搜索后的内容显示隐藏
const searchCard = ref(false)
//  搜索内容
const value = ref<string>('');
//  默认搜索站内
const resource = ref<string>('instation');
//  搜索placeholder显示
const placeholder = ref<string>('站内')
//  搜索后的url
const searchUrl = ref<string>('')
//  初始数据
const toolInfo = reactive(jsondata)
//  搜索后的数据
const searchInfo = ref([])
//  初始数据
const searchEngines = {
    instation: { placeholder: '站内搜索', searchUrl: '' },
    bing: { placeholder: '必应搜索', searchUrl: 'https://www.bing.com/search?q=' },
    baidu: { placeholder: '百度一下', searchUrl: 'https://www.baidu.com/s?wd=' },
    google: { placeholder: 'Google', searchUrl: 'https://www.google.com/search?q=' },
    sogou: { placeholder: '搜狗搜索', searchUrl: 'https://www.sogou.com/web?query=' },
    '360so': { placeholder: '360搜索', searchUrl: 'https://www.so.com/s?q=' },
    kaifa: { placeholder: '开发者搜索', searchUrl: 'https://kaifa.baidu.com/searchPage?wd=' },
};
//  图片加载失败显示的图片
const handleImageError = (event: Event) => {
    const target = event.target as HTMLImageElement;
    target.src = 'https://gitee.com/xiaomao12/images/raw/master/img/load_fail.png';
};

//  选择站点
const radioChange = (object) => {
    const selectedEngine = searchEngines[object.target.value];
    if (selectedEngine) {
        placeholder.value = selectedEngine.placeholder;
        searchUrl.value = selectedEngine.searchUrl;
    }
};

//  搜索
const onSearch = () => {
    if(value.value){
        const url = searchUrl.value + value.value;
        if(searchUrl.value){
            window.open(url, '_blank');
        }else{
            searchCard.value = true
            searchInfo.value.length = 0; // 清空之前的搜索结果
            toolInfo.forEach((e) => {
                e.children.forEach((v) => {
                    if (v.name.toLowerCase().includes(value.value.toLowerCase())) {
                        searchInfo.value.push(v);
                    }
                });
            });
        }
    }else{
        searchCard.value = false
    }
};
</script>

<style lang="less" scoped>
.column{
    width: 100%;
    height: 150px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    .search{
        width: 700px;
        margin-bottom: 20px;
    }
    .search_type{
        display: flex;
        justify-content: flex-start;
    }
}

h2 {
    font-weight: bold;
    margin-top: -15px;
}

img{
    width: 30px;
    height: 30px;
}

.children-container {
    display: flex;
    flex-wrap: wrap;

    /* 当屏幕小于一定宽度时，调整卡片宽度 */
    .a-card-grid {
        margin: 8px 8px;
        width: var(--width-text);
        height: 60px;
        display: flex;
        align-items: center;
        border-radius: 5px;
        font-weight: 600;
        color: var(--color-text);
    }

    /* 卡片文字的样式 */
    .card-text {
        white-space: nowrap;      /* 禁止换行 */
        overflow: hidden;         /* 隐藏溢出部分 */
        text-overflow: ellipsis;  /* 显示省略号 */
        text-align: center;       /* 居中文本 */
    }
    
    .card-text:hover::after {
        content: attr(data-tooltip); /* 显示 tooltip 的内容 */
        position: absolute;
        bottom: 100%; /* 显示在元素上方 */
        left: 50%;
        transform: translateX(-50%);
        background-color: rgba(0, 0, 0, 0.7);
        color: #fff;
        padding: 5px;
        border-radius: 4px;
        z-index: 10;
        white-space: normal;  /* 防止 tooltip 内容溢出 */
        max-width: 200px;     /* 设置最大宽度，防止 tooltip 过长 */
        word-wrap: break-word; /* 确保长文本在 tooltip 中换行 */
    }

    /* 媒体查询：当屏幕宽度小于 768px 时 */
    @media screen and (max-width: 768px) {
        .a-card-grid {
            width: 48%;  /* 在小屏幕上卡片宽度变为 48% */
        }
    }

    /* 媒体查询：当屏幕宽度小于 480px 时 */
    @media screen and (max-width: 480px) {
        .a-card-grid {
            width: 100%;  /* 在非常小的屏幕上每行只显示一个卡片 */
        }
    }
}
</style>
